import {
  coachList,
  drivingSchoolDetailById,
  evaluateList,
} from "../../../api/drivingSchool";
import { shareAppMessageSet } from "../../../utils/config";
import { dataCollection } from "../../../utils/dataCollection";
import { listProps, swiperProps } from "../../../utils/type";

var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    swiperList: <swiperProps[]>[],
    content: {
      tab: ["详情", "评价", "教练"],
    },
    drivingSchoolDetail: "", //驾校详情
    coach: <listProps>{
      current: 1,
      total: 0,
      list: [],
    }, //教练
    evaluateData: <listProps>{
      current: 1,
      total: 0,
      list: [],
    },
    selectTabIndex: 0,
    logo: "",
    drivingSchoolName: "",
    characteristic: <any[]>[],
    otherInfo: [
      { tag: "政府资质", msg: "", detail: "" },
      { tag: "发展历程", msg: "", detail: "" },
      { tag: "所获荣誉", msg: "", detail: "" },
    ],
    pageReady: false,
    themeCode: "",
    tabScollTop: 0,
    tabIsScoll: false,
    scrollTop: 0,
  },
  drivingSchoolId: "",
  coachPageCurrent: 1,
  t:0,
  onLoad(options: any) {
    //从本地获取当前选择的城市编码
    this.setData({
      themeCode: wx.getStorageSync("themeCode"),
    });

    this.drivingSchoolId = options.id;
    drivingSchoolDetailById(options.id).then((res) => {
      const otherInfoArray = [
        {
          tag: "政府资质",
          msg: res.result.governmentQualification[0].tag,
          detail: res.result.governmentQualification[0].msg,
        },
        {
          tag: "发展历程",
          msg: res.result.developHistory[0].tag,
          detail: res.result.developHistory[0].msg,
        },
        {
          tag: "所获荣誉",
          msg: res.result.honor[0].tag,
          detail: res.result.honor[0].msg,
        },
      ];
      const arr: swiperProps[] = [];
      res.result.schoolVideo.forEach((item: any) => {
        const obj: swiperProps = {
          type: "video",
          url: item.videoUrl,
          poster: item.coverUrl,
        };
        arr.push(obj);
      });
      res.result.schoolPic.forEach((item: any) => {
        const obj: swiperProps = {
          type: "image",
          url: item.picUrl,
        };
        arr.push(obj);
      });
      const a: any[] = [];
      res.result.schoolScale.map((item: any, index: number) => {
        if (index !== res.result.schoolScale.length - 1) {
          a.push(item, "");
        } else {
          a.push(item);
        }
      });
      this.setData({
        logo: res.result.logo,
        drivingSchoolName: res.result.fullName,
        characteristic: a,
        otherInfo: otherInfoArray,
        swiperList: arr,
        drivingSchoolDetail: res.result.details || "",
        pageReady: true,
      });
    });
    this.getEvaluateList();
    this.getCoachList();

   this.t = setTimeout(() => {
      const query = wx.createSelectorQuery();
      query.select("#carousel").boundingClientRect();
      query.selectViewport().scrollOffset();
      query.exec((res) => {
        console.log("carousel", res);
        this.setData({
          tabScollTop: res[0].height,
        });
      });
      const query2 = wx.createSelectorQuery();
      query2.select("#content-box").boundingClientRect();
      query2.selectViewport().scrollOffset();
      query2.exec((res) => {
        console.log("content-box", res);
        this.setData({
          tabScollTop: this.data.tabScollTop + res[0].height,
        });
      });
      clearTimeout(this.t)
    }, 500);
  },
  //监听选择的分类
  onSelectContentTab(res: any) {
    this.setData({
      selectTabIndex: res.currentTarget.dataset.index,
    });
    if (this.data.tabIsScoll) {
      this.setData({
        scrollTop: this.data.tabScollTop,
      });
    }
  },
  //打开展示页面
  onOpenPageShow(res: any) {
    wx.setStorageSync(
      "otherInfo",
      this.data.otherInfo[res.currentTarget.dataset.index].detail
    );
    wx.navigateTo({
      url:
        "../../../pages/drivingSchool/show/show?tag=" +
        this.data.otherInfo[res.currentTarget.dataset.index].tag,
    });
  },
  //获取教练列表
  getCoachList() {
    coachList(this.data.coach.current, this.drivingSchoolId).then((res) => {
      const list: any[] = this.data.coach.list;
      if (this.data.coach.current === 1) {
        list.length = 0;
      }
      list.push(...res.result.records);
      let index = this.data.coach.current;
      index++;
      const obj: listProps = {
        current: index,
        list: [...list],
        total: res.result.total,
      };
      this.setData({
        coach: obj,
      });
    });
  },
  //页面触底触发
  bindscrolltolower() {
    console.log("onReachBottom");
    if (
      this.data.coach.list.length < Number(this.data.coach.total) &&
      this.data.selectTabIndex === 2
    ) {
      this.getCoachList();
    } else if (
      this.data.evaluateData.list.length <
        Number(this.data.evaluateData.total) &&
      this.data.selectTabIndex === 1
    ) {
      this.getEvaluateList();
    }
  },
  n: 0,
  bindscroll(res: any) {
    if (
      res.detail.scrollTop >= this.data.tabScollTop &&
      this.n === 0 &&
      !this.data.tabIsScoll
    ) {
      this.n = 1;
      this.setData({
        tabIsScoll: true,
      });
    } else if (
      res.detail.scrollTop < this.data.tabScollTop &&
      this.n === 1 &&
      this.data.tabIsScoll
    ) {
      this.n = 0;
      this.setData({
        tabIsScoll: false,
      });
    }
  },
  //评价列表数据
  getEvaluateList() {
    evaluateList(this.drivingSchoolId, this.data.evaluateData.current).then(
      (res) => {
        const list: any[] = this.data.evaluateData.list;
        if (this.data.evaluateData.current === 1) {
          list.length = 0;
        }
        list.push(...res.result.records);
        let index = this.data.evaluateData.current;
        index++;
        const obj: listProps = {
          current: index,
          list: [...list],
          total: res.result.total,
        };
        this.setData({
          evaluateData: obj,
        });
      }
    );
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return { ...shareAppMessageSet() };
  },
  onHide() {
    dataCollection.setPageMapData("leave");
  },
  onUnload() {
    dataCollection.setPageMapData("leave");
  },
  onShow() {
    dataCollection.setPageMapData("enter", "驾校详情");
  },
  ongesture(res: any) {
    console.log("ongesture", res);
  },
});
